﻿using System;
using System.Data.OleDb;
using System.Windows.Forms;

namespace Texpro
{
    public partial class FMAD102 : Form
    {

        public FMAD102()
        {
            InitializeComponent();
        }

        private void frm_vendor_Load(object sender, EventArgs e)
        {
            load_form();
        }

        private void clear_form()
        {
            lst_vendor_type_id.SelectedIndex = -1;
            txt_vendor_id.Text = String.Empty;
            txt_vendor_name.Text = String.Empty;
            //txt_vendor_city.Text = String.Empty;
            lst_city_id.SelectedIndex = -1;
        }

        private void load_form()
        {
            lbl_form_title.Text = this.Text;
            PCPUB.m_fill_combo(lst_vendor_type_id, "VENDOR_TYPE");
            PCPUB.m_fill_combo(lst_city_id, "CITY");
            clear_form();
            switch (PCPUB.m_entry_form_mode)
            {
                case "ADDNEW":
                    txt_vendor_id.Enabled = true;
                    lst_vendor_type_id.Enabled = true;
                    txt_vendor_id.Focus();
                    break;
                case "EDIT":
                    txt_vendor_id.Enabled = false;
                    lst_vendor_type_id.Enabled = false;
                    fill_form((PCPUB.m_record_id));
                    lst_vendor_type_id.Focus();
                    break;
            }
        }

        private bool save_form()
        {
            try
            {
                String strQuery = String.Empty;
                OleDbConnection sqlConn = new OleDbConnection();
                OleDbCommand sqlComm = new OleDbCommand();
                //
                String vendor_type_id = String.Empty;
                String vendor_id = String.Empty;
                String vendor_name = String.Empty;
                String vendor_city = String.Empty;
                String vendor_city_id = String.Empty;
                //
                vendor_id = "'" + txt_vendor_id.Text.ToString() + "'";
                vendor_type_id = "'" + ((m_combo_item)lst_vendor_type_id.SelectedItem).combo_code.ToString() + "'";
                vendor_name = "'" + txt_vendor_name.Text.ToString() + "'";
                //vendor_city = "'" + txt_vendor_city.Text.ToString() + "'";
                vendor_city_id = "'" + ((m_combo_item)lst_city_id.SelectedItem).combo_code.ToString() + "'";
                vendor_city = "'" + ((m_combo_item)lst_city_id.SelectedItem).combo_text.ToString() + "'";
                //
                 switch (PCPUB.m_entry_form_mode)
                {
                    case "ADDNEW":
                        strQuery = String.Empty;
                        strQuery += "INSERT INTO [tis_vendor] (";
                        strQuery += "[vendor_type_id],";
                        strQuery += "[vendor_id],";
                        strQuery += "[vendor_name],";
                        strQuery += "[city],";
                        strQuery += "[city_id]";
                        strQuery += ") VALUES (";
                        strQuery += vendor_type_id + ",";
                        strQuery += vendor_id + ",";
                        strQuery += vendor_name + ",";
                        strQuery += vendor_city + ",";
                        strQuery += vendor_city_id + ")";
                        break;
                    case "EDIT":
                        strQuery += " UPDATE [tis_vendor] SET ";
                        //strQuery += " [vendor_type_id] = " + vendor_type_id + ",";
                        strQuery += " [vendor_name] = " + vendor_name + ",";
                        strQuery += " [city] = " + vendor_city + ",";
                        strQuery += " [city_id] = " + vendor_city_id;
                        strQuery += " WHERE [vendor_id] = " + vendor_id;
                        break;
                }
                //
                sqlConn.ConnectionString = PCPUB.m_oledb_connection.ToString();
                sqlConn.Open();
                sqlComm.Connection = sqlConn;
                sqlComm.CommandText = strQuery.ToString();
                sqlComm.ExecuteNonQuery();
                sqlConn.Close();
                PCPUB.m_admin_vendor_id = vendor_id;
                //
                sqlComm.Dispose();
                sqlConn.Dispose();
                return true;
            }
            catch (Exception e)
            {
                PCMSG.Show(e.Message);
                return false;
            }
        }

        private bool validate_form()
        {
            
            if (txt_vendor_id.Text == String.Empty)
            {
                PCMSG.Show("Vendor Id field should not be empty.","Vendor",MessageBoxIcon.Error);
                txt_vendor_id.Focus();
                return false;
            }
            else if (lst_vendor_type_id.SelectedIndex == -1)
            {
                PCMSG.Show("Vendor Type field should not be empty.","Vendor", MessageBoxIcon.Error);
                lst_vendor_type_id.Focus();
                return false;
            }
            else if (txt_vendor_name.Text == String.Empty)
            {
                PCMSG.Show("Vendor Name field should not be empty.","Vendor", MessageBoxIcon.Error);
                txt_vendor_name.Focus();
                return false;
            }
            //else if (txt_vendor_city.Text == String.Empty)
            //{
            //    PCMSG.Show("Vendor City field should not be empty.","Vendor", MessageBoxIcon.Error);
            //    txt_vendor_city.Focus();
            //    return false;
            //}
            else if (lst_city_id.SelectedIndex == -1)
            {
                PCMSG.Show("Vendor City field should not be empty.", "Vendor", MessageBoxIcon.Error);
                //txt_vendor_city.Focus();
                lst_city_id.Focus();
                return false;
            }
            return true;
        }

        private void but_save_Click(object sender, EventArgs e)
        {
            if (validate_form() == true)
            {
                if (save_form() == true)
                {
                    PCMSG.Show("Record saved successfully.","Vendor", MessageBoxIcon.Information);
                    switch (PCPUB.m_entry_form_mode)
                    {
                        case "ADDNEW":
                            clear_form();
                            PCPUB.m_admin_vendor_id = String.Empty;
                            txt_vendor_id.Focus();
                            break;
                        case "EDIT":
                            this.Close();
                            break;
                    }
                }
            }
        }

        private void but_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frm_vendor_KeyUp(object sender, KeyEventArgs e)
        {
            //if (e.KeyCode == Keys.Enter)
            //{
            //    e.Handled = false;
            //    SendKeys.Send("{TAB}");
            //}
        }

        private void fill_form(string p_id)
        {
            String strQuery = String.Empty;
            OleDbConnection sqlConn = new OleDbConnection();
            OleDbCommand sqlComm = new OleDbCommand();
            OleDbDataReader sqlRead;// = new OleDbDataReader();
            DateTime strReturn = DateTime.Now;
            //           
            sqlConn.ConnectionString = PCPUB.m_oledb_connection.ToString();
            sqlConn.Open();
            //
            strQuery = String.Empty;
            strQuery += " SELECT";
            strQuery += " [vendor_id],";
            strQuery += " [vendor_type_id],";
            strQuery += " [vendor_name],";
            strQuery += " [vendor_city],";
            strQuery += " [vendor_city_id]";
            strQuery += " FROM [vw_vendor]";
            strQuery += " WHERE [vendor_id] = '" + p_id + "'";
            //
            sqlComm.Connection = sqlConn;
            sqlComm.CommandText = strQuery.ToString();
            sqlRead = sqlComm.ExecuteReader();
            //
            if (sqlRead.Read())
            {
                txt_vendor_id.Text = sqlRead["vendor_id"].ToString();
                lst_vendor_type_id.SelectedIndex = lst_vendor_type_id.FindStringExact(PCPUB.m_get_vendor_type_name(sqlRead["vendor_type_id"].ToString()));
                txt_vendor_name.Text = sqlRead["vendor_name"].ToString();
                //txt_vendor_city.Text = sqlRead["vendor_city"].ToString();
                lst_city_id.SelectedIndex = lst_city_id.FindStringExact(PCPUB.m_city_name(sqlRead["vendor_city_id"].ToString()));
            }
            //
            sqlRead.Close();
            sqlConn.Close();
            sqlRead.Dispose();
            sqlComm.Dispose();
            sqlConn.Dispose();
        }

        private void text_id_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsLetterOrDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back))
            {
                e.Handled = true;
            }
        }

        private void text_gen_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsLetterOrDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Space || e.KeyChar == '.'))
            {
                e.Handled = true;
            }
        }

        private void text_int_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back))
            {
                e.Handled = true;
            }
        }

        private void text_dec_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == '.'))
            {
                e.Handled = true;
            }
        }


    }

}